<?php


namespace app\api\controller;

//http://cosmo.com/api/home/getNoticeDetails
use app\api\model\Adver;
use app\api\model\Notice;
use app\api\model\Slide;

class Home extends BaseApi {
    //幻灯片
    public function slide(){
        $slide = new Slide();
        $item = $slide->getSlide();
        if(!empty($item)){
            if($item->images){
                $imageArray = explode(',',$item->images);
                foreach ($imageArray as $image){
                    $imageList[] = $this->imageUrl.$image;
                }
                $item->imagesList = $imageList;
            }
            return json(['code'=>1,'data'=>$item]);
        }
        return json(['code'=>-1,'msg'=>'暂无数据']);
    }

    //广告图片
    public function adver(){
        $adver = new Adver();
        $adverList = $adver->getAdverList();

        if(!$adverList->isEmpty()){
            foreach ($adverList as $item){
                $item->image = $this->imageUrl.$item->image;
            }
            return json(['code'=>1,'data'=>$adverList]);
        }
        return json(['code'=>-1,'msg'=>'暂无数据']);
    }

    //公告
    public function notice(){
        if(!$this->request->param('page')){
            return json(['code'=>-1,'msg'=>'缺少参数']);
        }
        $page = max(1,intval($this->request->param('page')));
        $pagesize = intval($this->request->param('pagesize'));
        $notice = new Notice();
        $noticeList = $notice->getNoticeList($page,$pagesize);
        if(!$noticeList->isEmpty()){
            foreach ($noticeList as $list){
               $list->content = $this->replacePath($list->content);
            }
            return json(['code'=>1,'data'=>$noticeList]);
        }
        return json(['code'=>1,'data'=>'']);
    }

    //公告详情
    public function getNoticeDetails(){
        if(!$this->request->param('noticeid')){
            return json(['code'=>-1,'msg'=>'缺少参数']);
        }

        $noticeid = intval($this->request->param('noticeid'));
        $notice = new Notice();
        $noticeItem = $notice->where('id',$noticeid)->find();
        if(!empty($noticeItem)){
            $noticeItem->content = $this->replacePath($noticeItem->content);
            return json(['code'=>1,'data'=>$noticeItem]);
        }
        return json(['code'=>-1,'msg'=>'该公告不存在或已删除']);
    }



}